import pos_component from "../component/pos_component";
import { analysisIntent, baseScene, MapIntentConfig } from "../core/tool/baseScene";
import { mathType } from "../core/tool/face";
import { ComType } from "../core/tool/component";
import { PositionService } from "../service/PositionService";
import sdk from '../core/index';
import { TaskService } from "../service/TaskService";

export class scene extends baseScene {
    constructor() {
        super();
        this.map.name = 'test';
        this.map.enemyCfg = 'test';
        
        
        // 注册地图专属指令
        this.registerMapIntents([
            { command: '上', action: 'handleMoveUp', mathType: mathType.完全匹配 },
            { command: '下', action: 'handleMoveDown', mathType: mathType.完全匹配 },
            { command: '左', action: 'handleMoveLeft', mathType: mathType.完全匹配 },
            { command: '右', action: 'handleMoveRight', mathType: mathType.完全匹配 }
        ]);
    }

    async tick(data: analysisIntent): Promise<void> {
        let com_pos = data.player.getComponent(ComType.位置) as pos_component;
        let cell = this.map.checkPos(data.player);
    }

   

    // 位置（重写父类方法）
    onPosition(data: analysisIntent) {
        const result = PositionService.getPositionInfo(this.map, data.player);
        if (!result.success) {
            data.player.client_text(result.message);
            return;
        }
        
        // 获取位置信息卡片
        const card = result.card!;
        let admin = data.player.getComponent(ComType.base).hasAdminPermission();
        
        // 注册按钮意图
        this.registerButtonIntents(card, [
            { text: '移动', action: 'handleMove', mathType: mathType.完全匹配 },
            { text: '探索', action: 'handleExplore', mathType: mathType.完全匹配 },
            { text: '战斗', action: 'handleBattle', mathType: mathType.完全匹配 },
            { text: '侦察', action: 'handleSpy', mathType: mathType.开头匹配 },
            { text: '捕捉', action: 'handleCapture', mathType: mathType.开头匹配 },
            { text: '任务', action: 'taskbar', mathType: mathType.完全匹配 }
        ]);
        if(admin){
            this.registerButtonIntents(card, [
                { text: '回城', action: 'onBackHome', mathType: mathType.完全匹配 },
            ]);
        }
        
        data.player.client_text(card.getText());
    }
    

    

    // 战斗 - 使用新的 SDK 方式
    handleBattle(data: analysisIntent) {
        const battle = this.map.createBattleAll(data.player);
        if(!battle){
            return;
        }
        // 设置击杀回调
        battle.onKill((killLog: any, battleCore: any) => {
            // 处理击杀奖励
            const reward = sdk.prop.PolicyReward_击杀('野外击杀怪物', killLog, this.map.name);
            if (reward && reward.length > 0) {
                // 使用 battleCore.addReward 添加到 rewardMap，而不是直接加到背包
                battleCore.addReward(killLog.unit, reward);
            }
        });
        
        // 开始战斗
        battle.start((battleData: any) => {
            const clientCom = battleData.body.getComponent(ComType.client) as any;
            clientCom.client_text(battleData.card.getText());
        });
    }
}
export default new scene(); 